//
//  CYUserViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/9.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYUserViewController.h"
#import "CYDataSourceDelegate.h"
#import "CYUserHeader.h"
#import "CYUserCell.h"
#import "CYUserBtn.h"
#import "CYUserLBL.h"
#import "CYAccountDetailViewController.h"
#import "CYMyNoticeViewController.h"
#import "CYEnterDrawCashVC.h"
#import "CYPersonalModel.h"
#import "CYWebViewController.h"
@interface CYUserViewController (){
    CYDelegate *_delegate;
    CYDataSource *_datasource;
}
@property (nonatomic, strong) CYImageView *backImg;
@property (nonatomic, strong) CYButton *serviceBtn;//客服
@property (nonatomic, strong) CYButton *portrait;//个人头像
@property (nonatomic, strong) CYUserBtn *nameBtn;//名字
@property (nonatomic, strong) CYUserLBL *money;//金额
@property (nonatomic, strong) CYUserLBL *drawMoney;//提款金额
@property (nonatomic, strong) CYUserLBL *winMoney;//今日输赢
@property (nonatomic, strong) UITableView *tableview;
//@property (nonatomic, strong) NSDictionary *personal_data;//个人信息
@property (nonatomic, strong) CYPersonalModel *model;
//@property (nonatomic, strong) NSString *personal_plistName;//个人信息plist名称
@property (nonatomic, strong) NSMutableArray *arry;
@end

@implementation CYUserViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //    _personal_plistName = @"personal";
    self.backImg.hidden = self.serviceBtn.hidden = self.portrait.hidden = self.nameBtn.hidden = self.tableview.hidden = NO;
    self.money.money = self.drawMoney.money = self.winMoney.money = 0.00;
    
    self.model = [CYPersonalModel model:^id(__unsafe_unretained Class selfClas) {
        return [selfClas mj_objectWithFile:@"personal"];
        //    return [selfClas mj_objectWithKeyValues:dict];
    }];
    // Do any additional setup after loading the view.
}

/*懒加载*/
- (CYImageView *)backImg{
    if (!_backImg) {
        _backImg = [[CYImageView alloc]initWithImageName:@"userbag" size:CGSizeZero isRadius:NO];
        [self.view addSubview:_backImg];
        
        [_backImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0.0, 0.0, SCREEN_HEIGHT - AUTO(PT(359)), 0));
        }];
    }
    
    return _backImg;
}

- (CYButton *)serviceBtn{
    if (!_serviceBtn) {
        _serviceBtn = [[CYButton alloc]initWithTitle:@"客服" titleColor:[UIColor whiteColor] titleSize:AL(PT(24)) Image:@"serviceIcon"];
        [_serviceBtn addTarget:self action:@selector(clickServiceBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_serviceBtn];
        
        [_serviceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(AL(PT(64)));
            make.right.equalTo(self.view.mas_right).with.offset(-PT(30));
            make.size.mas_equalTo(CGSizeMake(AUTO(30), AUTO(30)));
        }];
    }
    
    return _serviceBtn;
}

- (CYButton *)portrait{
    if (!_portrait) {
        _portrait = [[CYButton alloc]initWithImage:@"portrait" cornerRadius:AUTO(PT(359 - 70 - 64 - 100) / 2.0)];
        _portrait.bounds = CGRectMake(0.0, 0.0, AUTO(PT(359 - 70 - 64- 100)), AUTO(PT(359 - 70 - 64- 100)));
        [_portrait addTarget:self action:@selector(changePortrait) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_portrait];
        
        [_portrait mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(AUTO(PT(115)));
            make.left.equalTo(self.view).with.offset(AUTO(PT(40)));
            make.size.mas_equalTo(_portrait.bounds.size);
        }];
    }
    
    return _portrait;
}

- (CYUserBtn *)nameBtn{
    if (!_nameBtn) {
        _nameBtn = [[CYUserBtn alloc]initWithMainTitle:@"彩票狗" secTitle:@"为幸运投注，赢美好未来" mainFont:20 secFont:11 color:[UIColor whiteColor] layout:mainTitleLeft];
        [_nameBtn addTarget:self action:@selector(changeUserName) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_nameBtn];
        
        [_nameBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_portrait.mas_right).with.offset(11);
            make.centerY.equalTo(_portrait);
            make.size.mas_equalTo(_nameBtn.bounds.size);
        }];
    }
    
    return _nameBtn;
}

- (CYUserLBL *)money{
    if (!_money) {
        _money = [[CYUserLBL alloc]initWithTitle:@"金额"];
        [self.view addSubview:_money];
        
        [_money mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(0);
            make.top.equalTo(self.view).with.offset(AUTO(PT(359 - 100)));
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 3.0, AUTO(PT(100))));
        }];
    }
    
    return _money;
}

- (CYUserLBL *)drawMoney{
    if (!_drawMoney) {
        _drawMoney = [[CYUserLBL alloc]initWithTitle:@"提款金额"];
        [self.view addSubview:_drawMoney];
        
        [_drawMoney mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(SCREEN_WIDTH / 3.0);
            make.top.equalTo(_money);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 3.0, AUTO(PT(100))));
        }];
    }
    
    return _drawMoney;
}

- (CYUserLBL *)winMoney{
    if (!_winMoney) {
        _winMoney = [[CYUserLBL alloc]initWithTitle:@"今日输赢"];
        [self.view addSubview:_winMoney];
        
        [_winMoney mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(SCREEN_WIDTH / 3.0 * 2.0);
            make.top.equalTo(_money);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 3.0, AUTO(PT(100))));
        }];
    }
    
    return _winMoney;
}
- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]init];
        [self.view addSubview:_tableview];
        [self setupTableview];
        
        [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(_backImg.mas_bottom).with.offset(0);
            make.bottom.equalTo(self.view).with.offset(-TABBAR_HEIGHT);
            make.width.mas_equalTo(SCREEN_WIDTH);
        }];
    }
    
    return _tableview;
}

- (void)setModel:(CYPersonalModel *)model{
    if (_model != model) {
        _model = model;
        _nameBtn.mainTitle = model.username;
        _winMoney.money = model.bonus;//今日输赢
        _money.money = model.coin;//余额
        _drawMoney.money = model.amount;//可提现金额
        [CYCache cache:[_model mj_JSONString] plist:@"personal"];
    }
}

- (void)setupTableview{
    WS(self)
    _datasource = [[CYDataSource alloc]initWithData:[self getCellSource] identifier:@"CYUserCell" block:^(id cell, id data) {
        [weakSelf bindCell:cell dict:data];
    }];
    
    _delegate = [[CYDelegate alloc]initWithCellHeight:AUTO(44) views:[CYUserHeader getHeader:[self getBtnTitleAndImage]] block:^(NSIndexPath *index) {
        [weakSelf selectedCell:index];
    }];
    
    _tableview.dataSource = _datasource;
    _tableview.delegate = _delegate;
    _tableview.scrollEnabled = NO;
    
}

- (void)bindCell:(CYUserCell *)cell dict:(NSDictionary *)dict{
    cell.img = dict[@"img"];
    cell.title = dict[@"title"];
}

- (void)selectedCell:(NSIndexPath *)index{
    
    NSString *VCName;
    if(index.section == 2 && index.row == 0)//更多
        VCName = @"CYMoreViewController";
    
    else if (index.section == 1){
        CYAccountDetailViewController *accountVC = [[CYAccountDetailViewController alloc] init];
        if (index.row == 0)//账户明细
            accountVC.type = AccountDetailType;
        else if (index.row == 1)//充值记录
            accountVC.type = RechargeRecordType;
        else if (index.row == 2)//提款记录
            accountVC.type = DrawRecordType;
        [self.navigationController pushViewController:accountVC animated:YES];
    }
    
    else if (index.section == 0){//我的消息
        if (index.row == 0) {
            CYMyNoticeViewController *noticeVC = [[CYMyNoticeViewController alloc]init];
            noticeVC.previousVCName = @"CYUserViewController";
            [self.navigationController pushViewController:noticeVC animated:YES];
        }
        else if (index.row == 1){//投注记录
            UITabBarController *tabVC  =  (UITabBarController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
            tabVC.selectedIndex = 2;
        }
    }
    
    Class vc = NSClassFromString(VCName);
    [self.navigationController pushViewController:[vc new] animated:YES];
    
}

#pragma mark - 按钮事件
/*响应用户头像按钮*/
- (void)changePortrait{
    
    
}
/*响应用户昵称按钮*/
- (void)changeUserName{
    
    
}
/*响应充值/提现按钮*/
- (void)rechargeOrDrawCash:(CYButton *)sender{
    NSString *vcName;
    if (sender.tag == 200){
        //        vcName = @"CYRechargeViewController";
        vcName = @"CYRechargeWayViewController";
        DeLog(@"选中充值方法");
    }
    else if(sender.tag == 201 ){
        if ([CYUser shareUser].bankName) {
            CYEnterDrawCashVC *drawVC = [[CYEnterDrawCashVC alloc] init];
            [self.navigationController pushViewController:drawVC animated:YES];
            return;
        }
        else
            vcName = @"CYDrawCashViewController";
        DeLog(@"选中提现方法");
    }
    Class vc = NSClassFromString(vcName);
    [self.navigationController pushViewController:[vc new] animated:YES];
}

/*响应客服按钮*/
- (void)clickServiceBtn{
    CYWebViewController *webVC = [[CYWebViewController alloc]init];
    webVC.title = @"客服";
    [self.navigationController pushViewController:webVC animated:YES];
    self.navigationController.navigationBar.hidden = NO;
    
}

//网络请求
- (void)httpsRquest{
    
    //银行卡是否绑定请求
    NSDictionary *dict = @{@"username":[CYUser shareUser].username,@"token":[CYUser shareUser].token};
//    NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:[CYUser shareUser].username,@"username",[CYUser shareUser].token,@"token",nil];
    if (![CYUser shareUser].bankName)
        self.spareTask = [CYHttps POST:API_isBindCart parameters:dict success:^(id data) {
            if ([data isKindOfClass:[NSDictionary class]]){
                CYUser *user = [CYUser mj_objectWithKeyValues:data];
                [CYUser shareUser].account = user.account;
                [CYUser shareUser].bankApiImgUrl = user.bankApiImgUrl;
                [CYUser shareUser].bankName = user.bankName;
                [CYUser shareUser].keTi = user.keTi;
                [CYUser shareUser].coin = user.coin;
            }
        } failure:nil];
    
    //个人中心请求
    self.task = [CYHttps POST:API_personal parameters:dict showHUD:YES progress:nil success:^(id data) {
        if ([data isKindOfClass:[NSDictionary class]])
            self.model = [CYPersonalModel mj_objectWithKeyValues:data];
        else {
            Class vc  = NSClassFromString(@"CYLoginViewController");
            [self.navigationController pushViewController:[vc new] animated:YES];
        }
    } failure:nil];
    
}

//判断是否登录
- (void)judgeIsLogin{
    if ([CYUser shareUser].token)
        [self httpsRquest];//如果登录，请求个人接口数据
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setupNavigationBarTintColor:[UIColor clearColor]];
    self.navigationController.navigationBar.hidden = YES;
    [self judgeIsLogin];//判断是否登录
}

- (NSArray *)getBtnTitleAndImage{
    
    return @[@{@"title":@"充值", @"img":@"recharge"},@{@"title":@"提现", @"img":@"tixian"}];
    
}

- (NSArray *)getCellSource{
    NSArray *array = @[@[@{@"title":@"我的消息",@"img":@"news"},@{@"title":@"投注记录",@"img":@"tzjl"}],
                       @[ @{@"title":@"账户明细",@"img":@"zhmx",},@{@"title":@"充值记录",@"img":@"czjl"},@{@"title":@"提款记录",@"img":@"tkjl"}],
                       @[@{@"title":@"更多",@"img":@"gd"}]];
    return array;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
